<template>
  <view class="content">
    <image class="logo" src="/static/logo.png" />
    <view class="text-area">
      <text class="title">{{ title }}</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import {getIPTVList} from "@/apis";

const title = ref('Hello')

const getList = async () => {
  const res = await getIPTVList()
  let playlistContent = convertToFormattedObjects([res])
  console.log('playlistContent', playlistContent)
}
// 解析M3U文件
function convertToFormattedObjects(playlist) {
  console.log('playlist', playlist)
  const formattedChannels = [];
  for (let i = 0; i < playlist.length; i += 2) {
    const infoLine = playlist[i].substring('#EXTM3U'.length); // Remove #EXTINF:-1 prefix
    const pattern = /#EXTINF:(.*),\s*(.*)(?:\r?\n|\r)(http.*)/g;
    let match;
    while ((match = pattern.exec(infoLine)) !== null) {
      const metadata = match[1];
      const title = match[2];
      const url = match[3];
      formattedChannels.push({metadata, title, url});
    }
  }
  formattedChannels.map(item => {
    const metaTest = item.metadata;
    let metaArr = metaTest.split(' ')
    metaArr.shift()
    item.metadata = this.arrayToObject(metaArr)
  })
  return formattedChannels;
},
function arrayToObject(array) {
  return array.reduce((obj, item) => {
    const [key, value] = item.split('=');
    obj[key.replace(/"/g, '')] = value.replace(/"/g, '');
    return obj;
  }, {});
}
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 200rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}
</style>
